<template>
  <div class="login-wrapper">
    <div class="modal">
      <el-form :model="loginForm" ref="login" :rules="rules">
        <div class="title">火星</div>
        <el-form-item prop="userName" >
          <el-input placeholder="请输入用户名" v-model.trim="loginForm.userName"  prefix-icon="el-icon-user"> </el-input>
        </el-form-item>
        <el-form-item prop="userPwd">
          <el-input placeholder="请输入密码" type="password" v-model.trim="loginForm.userPwd"  prefix-icon="el-icon-view"> </el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" class="btn-login" @click="submitLogin"  size="mini">登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>

export default {
  name: "Login",
  data() {
    return {
      //登录表单输入的数据
      loginForm : {
        userName : "",
        userPwd : ""
      },
      //登录表单校验的规则
      rules : {
        userName : [
          {required: true, message : "用户名不能为空", trigger: "blur"}
        ],
        userPwd : [
          {required: true, message : "密码不能为空", trigger: "blur"},
          {min : 6, max :20, message : "密码不能小于6位或者大于20位", trigger: "blur"}
        ]
      }
    };
  },
  created(){
    
  },
  methods : {
    //登录方法
    submitLogin(){
      //点击登录按钮,对登录表单进行校验
      this.$refs["login"].validate(async valid=>{
        if(valid){
          //调用登录接口
          let res = await this.userApi.login(this.loginForm);
          //将登录成功后的数据存储到vuex
          this.$store.commit("saveUserInfo",res)
          //跳转到主页
          this.$router.push("/")
        }else{
          return false;
        }
      })
    }
  },
  components: {},
};
</script>

<style scoped lang="scss">
.login-wrapper {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100vw;
  height: 100vh;
  background-color: #f9fcff;
  .modal {
    width: 500px;
    padding: 50px;
    background-color: #fff;
    border-radius: 5px;
    box-shadow: 0 0 10px 5px #c7c9cb4d;

    .title {
      font-size: 50px;
      line-height: 1.5;
      text-align: center;
      margin-bottom: 30px;
    }

    .btn-login{
        width:100%;
    }
  }
}
</style>
